package com.album.web.controller;

import com.album.common.cbb.RestResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @version v1.0
 * @ProjectName: zzm_video
 * @ClassName: UploadController
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Value("${filePath}")
    private String filePath;


    @RequestMapping("/image")
    public RestResult upload(@RequestParam("file") MultipartFile file) {
        RestResult<String> restResult = new RestResult<String>();
        if (file.isEmpty()) {
            restResult.setSuccess(false);
            restResult.setErrorMessage("没有文件");
            return restResult;
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" + size);

        File dest = new File(filePath + "/" + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest);
            restResult.setData(fileName);
            restResult.setSuccess(Boolean.TRUE);
            return restResult;
        } catch (Exception e) {
            restResult.setSuccess(Boolean.FALSE);
            return restResult;
        }
    }

}
